<template>
  <el-dialog
    title="分配角色"
    :visible.sync="dialogVisible"
    width="50"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" style="margin: 10px" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="5">
        <el-button size="mini" type="primary" @click="handleSet">提交</el-button>
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList, getUserDetail, setRole } from '@/api/employee'
export default {
  components: {},
  data() {
    return {
      id: '',
      dialogVisible: false,
      checkList: [],
      roleList: []
    }
  },
  methods: {
    async open(row) {
      this.id = row.id
      const res = await getUserDetail(this.id)
      // 获取当前员工的角色
      this.checkList = res.roleIds
      // 获取用户拥有的角色列表
      this.roleList = await getRoleList()
      this.dialogVisible = true
    },

    // 分配角色
    async handleSet() {
      await setRole({ id: this.id, roleIds: this.checkList })
      this.$message.success('角色分配成功')
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
